Skip to content

Method: create(TokenStream, Collection)

1: package de.fhdw.wtf.parser;
2:
3: import java.util.Collection;
4:
5: import de.fhdw.wtf.common.ast.type.MapType;
6: import de.fhdw.wtf.common.ast.type.Type;
7: import de.fhdw.wtf.common.exception.parser.AbstractParserException;
8: import de.fhdw.wtf.common.stream.TokenStream;
9: import de.fhdw.wtf.common.token.Token;
10:
11: /**
12: * Parser to parse the given TokenStream to {@link MapType}s.
13: */
14: final class MapParser {
15:         
16:         /**
17:          * TokenStream.
18:          */
19:         private final TokenStream stream;
20:         
21:         /**
22:          * Collection of exceptions.
23:          */
24:         private final Collection<AbstractParserException> exceptions;
25:         
26:         /**
27:          * Constructor of {@link MapParser}.
28:          *
29:          * @param stream
30:          * tokenStream
31:          * @param exceptions
32:          * collection of exceptions
33:          */
34:         private MapParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
35:                 this.stream = stream;
36:                 this.exceptions = exceptions;
37:         }
38:         
39:         /**
40:          * FactoryMethod for {@link MapParser}.
41:          *
42:          * @param stream
43:          * tokenStream
44:          * @param exceptions
45:          * collection of exceptions
46:          * @return The {@link MapParser}-Object.
47:          */
48:         static MapParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
49:                 return new MapParser(stream, exceptions);
50:         }
51:         
52:         /**
53:          * Parses a TokenStream to a MapType.
54:          *
55:          * @return Map
56:          * @throws AbstractParserException
57:          * AbstractParserException
58:          */
59:         MapType parse() throws AbstractParserException {
60:                 final Token firstToken = this.stream.next();
61:                 final Type key = TypeParser.create(this.stream, this.exceptions).parse();
62:                 ParserUtils.requireAndRemoveArrowToken(this.stream);
63:                 final Type value = TypeParser.create(this.stream, this.exceptions).parse();
64:                 ParserUtils.requireAndRemoveSquareBracketCloseToken(this.stream);
65:                 final MapType map = MapType.create(firstToken, key, value);
66:                 map.setLastToken(value.getLastToken());
67:                 return map;
68:         }
69:         
70: }